package com.example.sharding.jdbc.demo.job;

import com.example.sharding.jdbc.demo.domain.TestLog;
import com.example.sharding.jdbc.demo.mapper.TestLogMapper;
import com.example.sharding.jdbc.demo.util.DateUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author airtrioa
 * @date 2021/11/30
 */
@Component
public class StartUpJob implements CommandLineRunner {
  private static final Logger LOGGER = LoggerFactory.getLogger(StartUpJob.class);
  private final TestLogMapper testLogMapper;

  public StartUpJob(TestLogMapper testLogMapper) {
    this.testLogMapper = testLogMapper;
  }

  @Override
  public void run(String... args) throws Exception {
    // FIXME：表中所有的时间都是 2021.3.30,所以查询 2021.03.01 - 2021.03.31 ，如果分片策略生效则只能查出一条
    List<TestLog> list = testLogMapper.findList(
        DateUtil.toDate(LocalDateTime.parse("2021-03-01 12:12:12", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))),
        DateUtil.toDate(LocalDateTime.parse("2021-03-31 12:12:12", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
    LOGGER.info("条数:[{}],数据:[{}]", list.size(), new ObjectMapper().writeValueAsString(list));
  }
}
